

class Solution1 {
    int[] xx = {1, -1, 0, 0};
    int[] yy = {0, 0, 1, -1};
    boolean flag;
    boolean[][] st;

    public boolean exist(char[][] board, String word) {
        int n = board.length, m = board[0].length;
        st = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == word.charAt(0)) {
                    st[i][j] = true;
                    dfs(board, word, i, j, 1);
                    st[i][j] = false;
                }
            }
        }
        return flag;
    }

    public void dfs(char[][] board, String word, int x, int y, int u) {
        if (u == word.length()) {
            flag = true;
            return;
        }
        for (int i = 0; i < 4; i++) {
            int dx = x + xx[i];
            int dy = y + yy[i];
            if (dx >= 0 && dx < board.length && dy >= 0 && dy < board[0].length && word.charAt(u) == board[dx][dy] && !st[dx][dy]) {
                st[dx][dy] = true;
                dfs(board, word, dx, dy, u + 1);
                st[dx][dy] = false;
            }
        }
    }
}

class Solution {
    boolean[][] st;
    int n, m;
    int ret;
    int[] xx = {1, -1, 0, 0};
    int[] yy = {0, 0, 1, -1};
    int sum = 0;

    public int getMaximumGold(int[][] grid) {
        n = grid.length;
        m = grid[0].length;
        st = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] != 0) {
                    st[i][j] = true;
                    sum += grid[i][j];
                    dfs(grid, i, j);
                    st[i][j] = false;
                    sum -= grid[i][j];
                }
            }
        }
        return ret;
    }

    public void dfs(int[][] grid, int x, int y) {
        if(check(grid,x,y)) {
            ret = Math.max(ret,sum);
            System.out.println(ret);
            return;
        }
        for(int i =0 ; i<4; i++) {
            int dx = x + xx[i];
            int dy = y + yy[i];
            if(dx>=0 && dx<n && dy>=0 && dy<m && !st[dx][dy] && grid[dx][dy]!=0) {
                st[dx][dy] = true;
                sum += grid[dx][dy];
                dfs(grid,dx,dy);
                sum -= grid[dx][dy];
                st[dx][dy] = false;
            }
        }
    }

    public boolean check(int[][] grid, int x, int y) {
        for(int i = 0; i<4; i++) {
            int dx = x + xx[i];
            int dy = y + yy[i];
            if(dx>=0 && dx<n && dy>=0 && dy<m && !st[dx][dy] && grid[dx][dy]!=0) {
                return false;
            }
        }
        return true;
    }
}

public class Test {
    public static void main(String[] args) {
        int[][] grid = {{0,6,0},{5,8,7},{0,9,0}};
        int ret = new Solution().getMaximumGold(grid);
    }
    public static void main1(String[] args) {
        char[][] board = {{'a', 'a'}};
        boolean ret = new Solution1().exist(board, "aaa");

    }
}
